<template>
  <div class="downloads-container">
    <h2>我的下载</h2>
    
    <el-card class="downloads-card">
      <!-- 搜索筛选 -->
      <div class="filter-section">
        <el-input
          placeholder="搜索素材名称"
          v-model="searchQuery"
          style="width: 300px"
          clearable>
        </el-input>
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </div>

      <!-- 下载列表 -->
      <el-table :data="filteredDownloads" style="width: 100%">
        <el-table-column prop="name" label="素材名称"></el-table-column>
        <el-table-column prop="size" label="文件大小" width="120">
          <template #default="{row}">
            {{ formatSize(row.size) }}
          </template>
        </el-table-column>
        <el-table-column prop="time" label="下载时间" width="180"></el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{row}">
            <el-button type="text" @click="redownload(row)">重新下载</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      dateRange: [],
      downloads: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  computed: {
    filteredDownloads() {
      let list = [...this.downloads];
      if (this.searchQuery) {
        list = list.filter(item => 
          item.name.includes(this.searchQuery)
        );
      }
      if (this.dateRange && this.dateRange.length === 2) {
        const [start, end] = this.dateRange;
        list = list.filter(item => {
          const time = new Date(item.time).getTime();
          return time >= start.getTime() && time <= end.getTime();
        });
      }
      return list;
    }
  },
  methods: {
    formatSize(size) {
      if (size < 1024) return size + 'B';
      if (size < 1024 * 1024) return (size / 1024).toFixed(1) + 'KB';
      return (size / (1024 * 1024)).toFixed(1) + 'MB';
    },
    redownload(item) {
      console.log('重新下载:', item);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchDownloads();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchDownloads();
    },
    fetchDownloads() {
      // 模拟数据
      this.downloads = [
        {
          id: 1,
          name: '风景图片1.jpg',
          size: 2456789,
          time: '2023-05-01 10:30'
        },
        {
          id: 2,
          name: '商业模板.pptx',
          size: 12567890,
          time: '2023-05-02 14:15'
        }
      ];
      this.total = this.downloads.length;
    }
  },
  mounted() {
    this.fetchDownloads();
  }
}
</script>

<style scoped>
.downloads-container {
  padding: 20px;
}
.downloads-card {
  margin-top: 20px;
}
.filter-section {
  margin-bottom: 20px;
  display: flex;
  gap: 15px;
}
</style>